﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mail;

namespace StockMatch.Service
{
    /// <summary>
    /// C#邮件发送类 V1.0
    /// --Author:laifangsong QQ:25313644
    /// --最后修改：2007-07-27
    /// 
    /// 支持批量(发送、抄送、密送)
    /// 支持多语言、带多个附件
    /// 支持smtp发送邮件服务器验证
    /// </summary>
    public class DotNetSendMail
    {
        public DotNetSendMail()
        {
            AttachFiles = new ArrayList();
        }

        /// <summary>
        /// 邮件发送结果。如果发送过程出现错误，该值为捕获到的异常提示；否则，该值为“OK”。
        /// </summary>
        public string SendMailResult { get; set; }

        #region 邮件发送时，额外的功能属性设置    

        /// <summary>
        /// 抄送地址，多个以逗号隔开
        /// </summary>
        public string CC { get; set; }

        /// <summary>
        /// 密送地址，多个以逗号隔开
        /// </summary>
        public string BCC { get; set; }

        /// <summary>
        /// 邮件格式。可以赋值为“1”或“2”，“1”表示Html格式，“2”表示Text格式。
        /// </summary>
        [DefaultValue(1)]
        public int BodyFormat { get; set; }

        /// <summary>
        /// 邮件格式。可以赋值为“1”、“2”或“3”。
        ///“1”表示不紧急邮件，“2”表示普通邮件， “3”表示紧急邮件
        /// </summary>
        [DefaultValue(3)]
        public int MailPriority { get; set; }

        /// <summary>
        /// 附件地址列表，用该属性Add方法加入多个附件，附件地址必须是绝对路径
        /// </summary>
        public ArrayList AttachFiles { get; set; }
        #endregion


        /// <summary>
        /// 发送邮件方法,方法中几个参数是发邮件时所必须的
        /// </summary>
        /// <param name="from">发件人</param>
        /// <param name="to">邮件人，多个收件人逗号隔开</param>
        /// <param name="subject">邮件标题</param>
        /// <param name="body">正文</param>
        /// <param name="smtpServer">发送邮件服务器</param>
        /// <param name="smtpUsername">发件服务器登录名</param>
        /// <param name="smtpPassword">发件服务器登录密码</param>
        /// <returns></returns>
        public bool SendMail(string from, string to, string subject, string body, string smtpServer,
            string smtpUsername, string smtpPassword)
        {
            try
            {
                MailMessage mail = new MailMessage
                {
                    From = from,
                    To = to,
                    Subject = subject,
                    Body = body
                };

                SmtpMail.SmtpServer = smtpServer;
                //邮件服务器验证，需要输入您在邮件发送服务器上注册的邮箱用户名和密码
                if (smtpUsername != "")
                {
                    mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
                    mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", smtpUsername);
                    mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", smtpPassword);
                }
                //以上是发邮件时所必需的，下面是额外的一些功能设置

                if (this.CC != "")
                {
                    mail.Cc = this.CC;
                }
                if (this.BCC != "")
                {
                    mail.Bcc = this.BCC;
                }
                switch (this.BodyFormat)
                {
                    case 1:
                        mail.BodyFormat = MailFormat.Html;
                        break;
                    case 2:
                        mail.BodyFormat = MailFormat.Text;
                        break;
                    default:
                        break;
                }
                switch (this.MailPriority)
                {
                    case 1:
                        mail.Priority = System.Web.Mail.MailPriority.Low;
                        break;
                    case 2:
                        mail.Priority = System.Web.Mail.MailPriority.Normal;
                        break;
                    case 3:
                        mail.Priority = System.Web.Mail.MailPriority.High;
                        break;
                    default:
                        break;
                }
                if (this.AttachFiles.Count > 0)
                {
                    foreach (string file in AttachFiles)
                    {
                        if (file.Trim() != "")
                        {
                            mail.Attachments.Add(new MailAttachment(file.Trim()));
                        }
                    }
                }
                SmtpMail.Send(mail);
                SendMailResult = "OK";
                return true;
            }
            catch (Exception ex)
            {
                SendMailResult = ex.ToString();
                return false;
            }
        }
    }
}



